﻿using System.ComponentModel;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Jack.DataScience.Http.Jwt;
using Vav.Api.Utils;
using Vav.Api.GenericDtos;
using Vav.Data.TransferTypes;
using Newtonsoft.Json;
using Vav.Data.Models;
using Vav.Data.DataAccess;
using System;
using System.Collections.Generic;
using Vav.Data.Utils;
using Vav.Api.Authorization;
using Newtonsoft.Json.Linq;
using CarPool.Data.Models;
using System.Threading.Tasks;

namespace Vav.Api
{
    [Route("api/carpool")]
    public class CarPoolController : BaseController
    {
        private readonly CarPoolDataAccess carPoolDataAccess;
        private readonly GenericJwtToken genericJwtToken;

        public CarPoolController(CarPoolDataAccess carPoolDataAccess,
            GenericJwtToken genericJwtToken)
        {
            this.carPoolDataAccess = carPoolDataAccess;
            this.genericJwtToken = genericJwtToken;
        }
        [HttpPost, Route("add"), Auth]
        public ActionResult<ApiBaseModel> AddCarPool([FromBody]SysCarpool carPool)
        {
            carPool.Uid = genericJwtToken.UserId;
            var res = carPoolDataAccess.AddCarPool(carPool);
            if (res > 0) return ApiSuccess();
            return ApiFail("未找到该数据");
        }
        [HttpGet, Route("getlist"),]
        public ActionResult<ApiBaseModel> GetListCarPool(string keyword, int? type, int pageindex, int pagesize)
        {
            var result = carPoolDataAccess.GetListCarPool(keyword, type, pageindex, pagesize);
            return ApiSuccess(result);
        }
        [HttpGet, Route("get"),]
        public ActionResult<ApiBaseModel> GetCarPool(int? id)
        {
            var result = carPoolDataAccess.GetCarPool(id);
            if (result != null) return ApiSuccess(result);
            return ApiFail("未找到该数据");
        }
        [HttpGet, Route("mypublish"), Auth]
        public ActionResult<ApiBaseModel> GetListMyCarPool(int pageindex, int pagesize)
        {
            var result = carPoolDataAccess.GetListMyCarPool(genericJwtToken.UserId, pageindex, pagesize);
            return ApiSuccess(result);
        }
        [HttpGet, Route("asyncdata")]
        public async Task<ActionResult<ApiBaseModel>> AsyncData()
        {
            var data = await carPoolDataAccess.AsyncData();
            return ApiSuccess(data);
        }
        [HttpGet, Route("asyncdatacar")]
        public async Task<ActionResult<ApiBaseModel>> AsyncDataCar()
        {
            var data = await carPoolDataAccess.AsyncDataCar();
            return ApiSuccess(data);
        }
    }
}
